<?php
namespace app\lateCoach\controller;
use app\common\Base;
use app\common\Compent;
use app\common\Excel;
use think\Db;

class LateCoachTeacher extends Base {
	
	public function index() {
		return $this->fetch();
	}

	public function pageQuery() {
		$page = input('page');
		$rows = input('rows');
		$week = input("week");
		$coachType = input("coachType");
		$classId = input("classId");
		$teacherName = input("teacherName");
		$where["IS_VALID"] = 1;
		if($week) $where["WEEK"] = $week;
		if($coachType) $where["COACH_TYPE"] = $coachType;
		if($classId) $where["CLASS_ID"] = $classId;
		if($teacherName) $where["TEACHER_NAME"] = $teacherName;
		$result = Db::table("t_late_coach_plan")
				  ->page($page, $rows)
				  ->where($where)
				  ->order("WEEK,CLASS_ID,COACH_TYPE")
				  ->select();
		$count = Db::table("t_late_coach_plan")
				->where($where)
				->count();
		$list = [];
        foreach ($result as $key => $obj) {
            $week = $obj["WEEK"];
            if($week == 1) $obj["WEEK_TEXT"] = "星期一";
            else if($week == 2) $obj["WEEK_TEXT"] = "星期二";
            else if($week == 3) $obj["WEEK_TEXT"] = "星期三";
            else if($week == 4) $obj["WEEK_TEXT"] = "星期四";
            else if($week == 5) $obj["WEEK_TEXT"] = "星期五";
            else if($week == 6) $obj["WEEK_TEXT"] = "星期六";
            else if($week == 7) $obj["WEEK_TEXT"] = "星期日";
            $coachType = $obj["COACH_TYPE"];
            if($coachType == 1) $obj["COACH_TYPE_TEXT"] = "第八节";
            else if($coachType == 2) $obj["COACH_TYPE_TEXT"] = "晚修一";
            else if($coachType == 3) $obj["COACH_TYPE_TEXT"] = "晚修二";
            $list[$key] = $obj;
        }
		$grid['rows'] = $list;
		$grid['total'] = $count;
		return $grid;
	}

	public function addUpdate() {
		$all = request()->param();
		try {
			if(array_key_exists("ID", $all)) {
				update('t_late_coach_plan', $all);
			}
			return "000";
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return "999";
	}

	function importExcel() {
		$examId = input("examId");
		$subject = input("subject");
	    //上传excel文件
	    $file = request()->file('files');
	    if(!$file) {
	    	return "请选择文件";
	    }
	    $data = Excel::importExcel($file);
	    if(count($data) == 0) {
	    	return "没有数据！";
	    }
	    try {
    		$errorList = [];
    		$result = [];
    		foreach ($data as $key => $obj) {
    			$error = [];
    			$week = $this::getWeek($obj["星期"]);
    			$coachType = $this::getCoachType($obj["时段"]);
    			if(!$week) array_push($error,"星期的值有误");
    			if(!$coachType) array_push($error,"时段的值有误");
    			if(count($error) > 0) {
    				array_push($errorList,"第".$key."列".implode("|", $error));
    			}else {
    				$itm["WEEK"] = $week;
    				$itm["CLASS_ID"] = $obj["班级"];
    				$itm["COACH_TYPE"] = $coachType;
    				$itm["TEACHER_NAME"] = $obj["教师"];
    				$result[$key] = $itm;
    			}
    		}
    		if(count($errorList) == 0) {
				Db::table('t_late_coach_plan')->insertAll($result);
    		}else {
    			return implode(",", $errorList);
    		}
	    } catch (\Exception $e) {
	    	return $e->getMessage();
	    }
	    return "000";
	}

	function getWeek($v) {
		$array = array("星期一"=>"1","星期二"=>"2","星期三"=>"3","星期四"=>"4","星期五"=>"5","星期六"=>"6","星期日"=>"7");
		if(array_key_exists($v, $array)) {
			return $array[$v];
		}
		return null;
	}

	function getCoachType($v) {
		$array = array("第八节"=>"1","晚修一"=>"2","晚修二"=>"3");
		if(array_key_exists($v, $array)) {
			return $array[$v];
		}
		return null;
	}

}
